home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Mac2Win / Sample / MiniEdit(original) / BuggyEdit.c next >
Encoding:
C/C++ Source or Header  |  1997-06-26  |  6.5 KB  |  328 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     Buggy MiniEdit.c
  4.     
  5.     The sample application from Inside Macintosh (RoadMap p.15-17)
  6.     beefed up a bit by Stephen Z. Stein, Symantec Corp.
  7.     Use this file with the “MiniEdit” chapter of your manual.
  8.     
  9.     The resources used in this program are in the file MiniEdit.rsrc.
  10.         
  11.     *** There is a bug in this file! ***
  12.     
  13. *********************************************************************/
  14.  
  15. #include "mini.file.h"
  16. #include "mini.windows.h"
  17. #include "MiniEdit.h"
  18. #include <TextEdit.h>
  19. #include <Fonts.h>
  20. #include <Events.h>
  21. #include <Dialogs.h>
  22. #include <Resources.h>
  23. #include <Scrap.h>
  24. #include <ToolUtils.h>
  25. #include <Devices.h>
  26.  
  27. WindowRecord    wRecord;
  28. WindowPtr        myWindow;
  29. TEHandle        TEH;
  30. int                linesInFolder;
  31. Rect            dragRect = { 0, 0, 1024, 1024 };
  32. MenuHandle        myMenus[3];
  33. ControlHandle     vScroll;
  34. Cursor            editCursor;
  35. Cursor            waitCursor;
  36. char            dirty;
  37.  
  38. extern Str255     theFileName;
  39.  
  40. #define    ours(w)        ((myWindow != NULL) && (w == myWindow))
  41.  
  42. #if !(defined(THINK_C) || defined(THINKC_PLUS))
  43. extern QDGlobals    qd;
  44. #endif
  45.  
  46. void main() 
  47. {
  48.     int        myRsrc;
  49.     
  50.     InitGraf(&qd.thePort);
  51.     InitFonts()    ;            /* <----- missing semicolon is the 'bug' */
  52.     FlushEvents(everyEvent, 0);
  53.     InitWindows();                
  54.     InitMenus();
  55.     TEInit();
  56.     InitDialogs(0L);
  57.     InitCursor();
  58.     MaxApplZone();
  59.  
  60. /*
  61.  *  The following statement is included as a check to see if we can
  62.  *    access our program's resources. 
  63.  */
  64.     
  65.     if (GetResource('MENU', fileID)==0) {
  66.         SysBeep(20);
  67.         CantOpen();
  68.         return;
  69.     }
  70.     
  71.     SetUpFiles();
  72.     SetUpCursors();
  73.     SetUpMenus();
  74.     SetUpWindows();
  75.     while (MainEvent()) ;
  76. }
  77.  
  78.  
  79.  
  80. int DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  81.  
  82. {
  83.     int retVal;
  84.     
  85.     switch (windowPart) {
  86.         case inGoAway:
  87.             if (ours(whichWindow))
  88.                 if (TrackGoAway(myWindow, myEvent->where))
  89.                     DoFile(fmClose);
  90.             break;
  91.  
  92.         case inMenuBar:
  93.             retVal = (DoCommand(MenuSelect(myEvent->where)));
  94.  
  95.         case inSysWindow:
  96.             SystemClick(myEvent, whichWindow);
  97.             break;
  98.  
  99.         case inDrag:
  100.             if (ours(whichWindow))
  101.                 DragWindow(whichWindow, myEvent->where, &dragRect);
  102.             break;
  103.  
  104.         case inGrow:
  105.             if (ours(whichWindow))
  106.                 MyGrowWindow(whichWindow, myEvent->where);
  107.             break;
  108.  
  109.         case inContent:
  110.             if (whichWindow != FrontWindow())
  111.                 SelectWindow(whichWindow);
  112.             else if (ours(whichWindow))
  113.                 DoContent(whichWindow, myEvent);
  114.             break;
  115.     }
  116.     
  117.     return retVal;
  118. }
  119.  
  120.  
  121. int MainEvent(void) 
  122. {
  123.     EventRecord        myEvent;
  124.     WindowPtr        whichWindow;
  125.     short            windowPart;
  126.     Rect            r;
  127.     
  128.     MaintainCursor();
  129.     MaintainMenus();
  130.     SystemTask();
  131.     TEIdle(TEH);
  132.     if (GetNextEvent(everyEvent, &myEvent)) {
  133.         switch (myEvent.what) {
  134.         case mouseDown:
  135.             windowPart = FindWindow(myEvent.where, &whichWindow);
  136.             DoMouseDown(windowPart, whichWindow, &myEvent);
  137.             break;
  138.  
  139.         case keyDown:
  140.         case autoKey: 
  141.             {
  142.             register char    theChar;
  143.             
  144.             theChar = myEvent.message & charCodeMask;
  145.             if ((myEvent.modifiers & cmdKey) != 0) 
  146.                 return(DoCommand(MenuKey( theChar)));
  147.             else {
  148.                 TEKey(theChar, TEH);
  149.                 ShowSelect();
  150.                 dirty = 1;
  151.                 }
  152.             }
  153.             break;
  154.  
  155.         case activateEvt:
  156.             if (ours((WindowPtr)myEvent.message)) {
  157.                 if (myEvent.modifiers & activeFlag) {
  158.                     TEActivate(TEH);
  159.                     ShowControl(vScroll);
  160.                     DisableItem(myMenus[editM], undoCommand);
  161.                     TEFromScrap();
  162.                 }
  163.                 else {
  164.                     TEDeactivate(TEH);
  165.                     HideControl(vScroll);
  166.                     ZeroScrap();
  167.                     TEToScrap();
  168.                 }
  169.             }
  170.             break;
  171.  
  172.         case updateEvt: 
  173.             if (ours((WindowPtr) myEvent.message))
  174.                     UpdateWindow(myWindow);
  175.             break;
  176.         } /* end of case myEvent.what */
  177.     } /* if */
  178.     return(1);
  179. }
  180.  
  181. void SetUpMenus(void)
  182. {
  183.     int        i;
  184.     
  185.     myMenus[appleM] = NewMenu(appleID, "\p\024");
  186.     AddResMenu(myMenus[appleM], 'DRVR');
  187.     myMenus[fileM] = GetMenu(fileID);
  188.     myMenus[editM] = GetMenu(editID);
  189.     for ((i=appleM); (i<=editM); i++)
  190.         InsertMenu(myMenus[i], 0) ;
  191.     DrawMenuBar();
  192. }
  193.  
  194. int DoCommand(long mResult)
  195.  
  196. {
  197.     int        theItem;
  198.     Str255    name;
  199.     
  200.     theItem = LoWord(mResult);
  201.     switch (HiWord(mResult)) {
  202.         case appleID:
  203.             GetItem(myMenus[appleM], theItem, name);
  204.             OpenDeskAcc(name);
  205.             SetPort(myWindow);
  206.             break;
  207.  
  208.         case fileID: 
  209.             DoFile(theItem);
  210.             break;
  211.  
  212.         case editID: 
  213.             if (SystemEdit(theItem-1) == 0) {
  214.                 switch (theItem) {
  215.                     case cutCommand:
  216.                         TECut(TEH);
  217.                         dirty = 1;
  218.                         break;
  219.     
  220.                     case copyCommand:
  221.                         TECopy(TEH);
  222.                         break;
  223.         
  224.                     case pasteCommand:
  225.                         TEPaste(TEH);
  226.                         dirty = 1;
  227.                         break;
  228.         
  229.                     case clearCommand:
  230.                         TEDelete(TEH);
  231.                         dirty = 1;
  232.                         break;
  233.                 }
  234.                 ShowSelect();
  235.             }
  236.             break;
  237.     }
  238.     HiliteMenu(0);
  239.     return(1);
  240. }
  241.  
  242. void MaintainCursor(void)
  243. {
  244.     Point        pt;
  245.     WindowPeek    wPtr;
  246.     GrafPtr        savePort;
  247.     
  248.     if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
  249.         GetPort(&savePort);
  250.         SetPort((GrafPtr)wPtr);
  251.         GetMouse(&pt);
  252.         if (PtInRect(pt, &(**TEH).viewRect ) )
  253.             SetCursor( &editCursor);
  254.         else SetCursor(&qd.arrow);
  255.         SetPort(savePort);
  256.     }
  257. }
  258.  
  259. void MaintainMenus(void)
  260. {
  261.     if ( !(*(WindowPeek)myWindow).visible || 
  262.             !ours(FrontWindow()) ) {
  263.         EnableItem(myMenus[fileM], fmNew);
  264.         EnableItem(myMenus[fileM], fmOpen);
  265.         DisableItem(myMenus[fileM], fmClose);
  266.         DisableItem(myMenus[fileM], fmSave);
  267.         DisableItem(myMenus[fileM], fmSaveAs);
  268.         DisableItem(myMenus[fileM], fmRevert);
  269.         DisableItem(myMenus[fileM], fmPrint);
  270.         EnableItem(myMenus[editM], undoCommand);
  271.         EnableItem(myMenus[editM], cutCommand);
  272.         EnableItem(myMenus[editM], copyCommand);
  273.         EnableItem(myMenus[editM], clearCommand);
  274.     }
  275.     else {
  276.         DisableItem(myMenus[fileM], fmNew);
  277.         DisableItem(myMenus[fileM], fmOpen);
  278.         EnableItem(myMenus[fileM], fmClose);
  279.         EnableItem(myMenus[fileM], fmSaveAs);
  280.         EnableItem(myMenus[fileM], fmPrint);
  281.         if (dirty && theFileName[0] != 0) {
  282.             EnableItem(myMenus[fileM], fmRevert);
  283.             EnableItem(myMenus[fileM], fmSave);
  284.         }
  285.         else {
  286.             DisableItem(myMenus[fileM], fmRevert);
  287.             DisableItem(myMenus[fileM], fmSave);
  288.         }
  289.         DisableItem(myMenus[editM], undoCommand);
  290.         if ((**TEH).selStart==(**TEH).selEnd) {
  291.             DisableItem(myMenus[editM], cutCommand);
  292.             DisableItem(myMenus[editM], copyCommand);
  293.             DisableItem(myMenus[editM], clearCommand);
  294.         }
  295.         else {
  296.             EnableItem(myMenus[editM], cutCommand);
  297.             EnableItem(myMenus[editM], copyCommand);
  298.             EnableItem(myMenus[editM], clearCommand);
  299.         }
  300.     }
  301. }
  302.  
  303. void SetUpCursors(void)
  304. {
  305.     CursHandle    hCurs;
  306.     
  307.     hCurs = GetCursor(1);
  308.     editCursor = **hCurs;
  309.     hCurs = GetCursor(watchCursor);
  310.     waitCursor = **hCurs;
  311. }
  312.  
  313.  
  314. void CantOpen(void)
  315. {
  316.     Rect r;
  317.  
  318.     SetRect(&r, 152, 60, 356, 132);
  319.     SetPort((myWindow = NewWindow( (Ptr) 0L, &r, "\p", true, dBoxProc, (WindowPtr) -1L, false, 0L)));
  320.     TextFont(0);
  321.     MoveTo(4, 20);
  322.     DrawString("\pCan't open resource file.");
  323.     MoveTo(4, 40);
  324.     DrawString("\pClick mouse to exit.");
  325.     do {
  326.     } while (!Button());
  327. }
  328.